﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;
using MyFrame.Cms.Model;
using MyFrame.Core.DBUtility;



namespace MyFrame.AutoCode
{
    /// <summary>
    /// 数据访问类:文章内容
    /// </summary>
    public  class CmsArticleDAL2
    {

        public CmsArticleDAL2()
        {
        }

        /// <summary>
        /// [增加]一条数据
        /// </summary>
        /// <param Name="model">数据对象</param>
        /// <returns>插入后数据的流水号 0 表示插入失败</returns>
        public static int Add(CmsArticleEntity model)
        {
            string sql = @"INSERT INTO CmsArticle
                                (
                                    Title,
                                    AddTime
                                )
                                VALUES
                                (
                                    @Title,
                                    @AddTime
                                )
                    ;SELECT @@IDENTITY";
            SqlParameter[] parameters = {
					                    new SqlParameter("@Title",model.Title),
                                        new SqlParameter("@AddTime",model.Addtime)
                                        };
            object value = DbHelperSQL.GetSingle(sql, parameters);
            if (value != null)
            {
                return Int32.Parse(value.ToString());
            }
            return 0;
        }

        /// <summary>
        /// [查询]根据id获得对象
        /// </summary>
        /// <param Name="id">编号</param>
        /// <returns>实体对象</returns>
        public static CmsArticleEntity GetCmsArticle(int id) 
        {

            string sql = @"SELECT * FROM CmsArticle WHERE Id=@Id";
            SqlParameter[] parameters = {
                                            new SqlParameter("@Id",id)
                                        };
            DataSet ds =DbHelperSQL.Query(sql, parameters);
            if (ds.Tables[0].Rows.Count > 0) 
            {
                DataRowToEntity(ds.Tables[0].Rows[0]);
            }
            return null;
        }



        /// <summary>
        /// [查询]单条件查询
        /// </summary>
        /// <param name="queryParam">单个条件查询参数</param>
        /// <returns>对象集合</returns>
        public static List<CmsArticleEntity> QueryCmsArticleList(QueryParam queryParam)
        {
            List<QueryParam> queryParamList = new List<QueryParam>();
            queryParamList.Add(queryParam);
            return QueryCmsArticleList(queryParamList, null, null);
        }

        /// <summary>
        /// [查询]带排序、分页单条件查询
        /// </summary>
        /// <param name="queryParam">单个条件查询参数</param>
        /// <param name="queryOrder">单个查询排序</param>
        /// <param name="queryPage">分页</param>
        /// <returns>对象集合</returns>
        public static List<CmsArticleEntity> QueryCmsArticleList(QueryParam queryParam, QueryOrder queryOrder,QueryPage queryPage)
        {
            List<QueryParam> queryParamList = new List<QueryParam>();
            queryParamList.Add(queryParam);
            List<QueryOrder> queryOrderList = new List<QueryOrder>();
            queryOrderList.Add(queryOrder);
            return QueryCmsArticleList(queryParamList, queryOrderList, queryPage);
        }

        /// <summary>
        /// [查询]带分页多条件、多排序查询
        /// </summary>
        /// <param name="queryParamList">查询参数集合</param>
        /// <param name="queryOrderList">排序参数集合</param>
        /// <param name="queryPage">分页</param>
        /// <returns>对象集合</returns>
        public static List<CmsArticleEntity> QueryCmsArticleList(List<QueryParam> queryParamList,List<QueryOrder> queryOrderList, QueryPage queryPage)
        {

            string where = " WHERE 1=1";
            string count ="";
            string sql = "";
            SqlParameter[] parameters;

            //构造查询参数
            parameters = UtilsHelper.BuildSqlParameters(queryParamList);

            //构造查询条件
            where = UtilsHelper.BuildWhere(queryParamList);

            //查询对象总数
            count = "SELECT COUNT(*) FROM CmsArticle "+ where;
            queryPage.ObjectCount = Convert.ToInt32(DbHelperSQL.GetSingle(count, parameters));

            
            //获得查询对象集合
            sql = UtilsHelper.BuildQueryPageSql("CmsArticle",queryParamList, queryOrderList, queryPage);


            DataSet dataSet = DbHelperSQL.Query(sql, parameters);
            if (dataSet.Tables[0].Rows.Count > 0) 
            {
                return RowsToList(dataSet.Tables[0].Rows);
            }
            return null;

        }

        /// <summary>
        /// 将一行数据转换为对象
        /// </summary>
        /// <param name="dataRow">一行数据</param>
        /// <returns>一个数据对象</returns>
        private static CmsArticleEntity DataRowToEntity(DataRow dataRow)
        {
            CmsArticleEntity entity = new CmsArticleEntity();
            DataRow row = dataRow;
            if((row["Id"]!=null)&&(row["Id"].ToString() != ""))
            {
                entity.Id = int.Parse(row["Id"].ToString());
            }

            if ((row["Title"] != null) && (row["Title"].ToString() != ""))
            {
                entity.Title = row["Title"].ToString();
            }

            if ((row["AddTime"] != null) && (row["AddTime"].ToString() != ""))
            {
                entity.Addtime = DateTime.Parse(row["AddTime"].ToString());
            }
            return entity;
        }


        /// <summary>
        /// 将多行数据转化为对象集合
        /// </summary>
        /// <param name="dataRows">多行数据</param>
        /// <returns>对象集合</returns>
        private static List<CmsArticleEntity> RowsToList(DataRowCollection dataRows) 
        {
            int count = dataRows.Count;
            List<CmsArticleEntity> list = new List<CmsArticleEntity>();
            for (int i = 0; i < count; i++)
            {
                CmsArticleEntity entity = DataRowToEntity(dataRows[i]);
                list.Add(entity);
            }
            return list;
        }

    }
}

